"""
@Time ： 2023/7/16 0009 10:07
@Auth ： zpt
@File ：ConfigPage.py
@IDE ：PyCharm
"""
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from Page.LoginPage import LoginPage
from selenium.webdriver.common.keys import Keys
from Lib.common_lib import LOG_INFO, LOG_ERROR


class config_information(object):
    """镜像定制页面"""

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def enter_config_overview(self):
        """
          进入config 页签
         """
        try:
            self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[2]/div[3]').click()

        except Exception as e:
            message = 'config tab 按钮定位失败'
            LOG_ERROR("error: found config tab button timeout")
            print(e)
            LOG_INFO(message)

    def check_config_view(self):
        """
          检查config页面元素
         """
        try:
            build_targets = self.driver.find_element(By.XPATH, '//div/div[1]/div/div[1]/div[1]/p[@class="config-title"]').text
            add_build_target = self.driver.find_element(By.XPATH, '//div[@class="config-left"]/div[1]/div/div[1]/div['
                                                              '1]/button/span').text
            os_variant = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table from-config"]/div/div['
                                                        '1]/div[2]/table/thead/tr/th[1]/div').text
            arch = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table from-config"]/div/div[1]/div['
                                                  '2]/table/thead/tr/th[2]/div').text
            ground_project = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table from-config"]/div/div['
                                                            '1]/div[2]/table/thead/tr/th[3]/div').text
            build_flag = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table from-config"]/div/div['
                                                        '1]/div[2]/table/thead/tr/th[4]/div').text
            publish_flag = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table from-config"]/div/div['
                                                          '1]/div[2]/table/thead/tr/th[5]/div').text
            operation_target = self.driver.find_element(By.XPATH, '//div[@class="build-targets-table '
                                                              'from-config"]/div/div[1]/div[2]/table/thead/tr/th['
                                                              '6]/div').text
            rpm_repo = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                                      '2]/div[1]/h3').text
            add_rpm_repo = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                          '1]/div[2]/div[1]/button/span').text
            name = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                                  '2]/div[2]/div/div[1]/div[2]/table/thead/tr/th[1]/div').text
            repo = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                                  '2]/div[2]/div/div[1]/div[2]/table/thead/tr/th[2]/div').text
            operation_repo = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                            '1]/div[2]/div[2]/div/div[1]/div[2]/table/thead/tr/th['
                                                            '3]/div').text
            layer_urls = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                        '1]/div[3]/div[1]/h3').text
            add_layer_url = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                           '1]/div[3]/div[1]/button/span').text
            label_for_url = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                           '1]/div[3]/div[2]/div/div[1]/div[2]/table/thead/tr/th['
                                                           '1]/div').text
            url = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                                 '3]/div[2]/div/div[1]/div[2]/table/thead/tr/th[2]/div').text
            operation_layer = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                             '1]/div[3]/div[2]/div/div[1]/div[2]/table/thead/tr/th['
                                                             '3]/div').text
            config =  self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[2]/p').text
            return [build_targets,add_build_target, os_variant, arch, ground_project, build_flag, publish_flag, operation_target,
                rpm_repo, add_rpm_repo, name, repo, operation_repo, layer_urls, add_layer_url, label_for_url, url, operation_layer ,config]
        except Exception as e:
            message = 'config 页面元素定位失败'
            LOG_ERROR("error: found config tab element timeout")
            print(e)
            LOG_INFO(message)


    def check_add_target_view(self):
        """
        build target 弹窗页面信息校验
        """
        time.sleep(3)
        # 点击config 页签
        self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[2]/div[3]').click()
        time.sleep(3)
        # 点击添加构建目标
        self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/div[1]/div['
                                     '1]/button/span').click()
        try:
            os_variant = self.driver.find_element(By.XPATH, '//div/div[1]/div/div/div/input[@placeholder="请选择构建环境"]')
            ground_projects = self.driver.find_element(By.XPATH, '//div[@aria-label="添加构建目标"]/div/div/form/div[2]/div/div/div/div[1]/input ')
            arch = self.driver.find_element(By.XPATH, '//input[@placeholder="请选择架构"]')
            build_flag = self.driver.find_element(By.XPATH, '//span[2][text()="打开构建"]')
            publish_flag = self.driver.find_element(By.XPATH, '//span[2][text()="打开发布"]')
            ok_btn = self.driver.find_element(By.XPATH, '//div[@aria-label="添加构建目标"]/div/footer/span/button[1]/span')
            cancel_btn = self.driver.find_element(By.XPATH, '//div[@aria-label="添加构建目标"]/div/footer/span/button[2]/span')
            close_btn = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                           '1]/div[1]/div/div[2]/div/div/header/button')
            element_list = [os_variant, ground_projects, arch, build_flag, publish_flag, ok_btn, cancel_btn, close_btn]
            return element_list
        except Exception as e:
            message = 'config 页面添加构建目标弹窗元素定位失败'
            LOG_ERROR("error: configuration page add build target pop-up element positioning timeout")
            print(e)
            LOG_INFO(message)

    def check_add_target_cancel_btn(self):
        """
          add build targrt 弹窗取消按钮
         """
        try:
            self.driver.find_element(By.XPATH, '//div[@aria-label="添加构建目标"]/div/footer/span/button[2]').click()

        except Exception as e:
                message = 'config 页面添加构建目标弹窗取消按钮定位失败'
                LOG_ERROR("error: configuration page add build target cancel button positioning timeout")
                print(e)
                LOG_INFO(message)

    def check_add_target_close_btn(self):
        """
          add build targrt 弹窗关闭按钮
         """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div['
                                                           '1]/div[1]/div/div[2]/div/div/header/button').click()

        except Exception as e:
                message = 'config 页面添加构建目标弹窗关闭按钮定位失败'
                LOG_ERROR("error: configuration page add build target close button positioning timeout")
                print(e)
                LOG_INFO(message)

    def check_target_no_data(self):
        """
          没有添加构建目标时数据检查
         """
        try:
            info = self.driver.find_element(By.XPATH, '//div/div[1]/div[2]/div/div[1]/div[3]/div/div[1]/div/div/span[text('
                                               ')="暂无数据"]').text
            return info
        except Exception as e:
                message = 'config 页面暂无数据文本定位失败'
                LOG_ERROR("error: configuration page add target no data positioning timeout")
                print(e)
                LOG_INFO(message)

    def check_add_build_target(self):
        """
       添加build_target
       """
        try:
            time.sleep(3)
            # 点击config 页签
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[2]/div[3]').click()
            time.sleep(3)
            # 点击添加构建目标
            self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/div[1]/div['
                                     '1]/button/span').click()
            time.sleep(5)
            # 点击OS Variant下拉框
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                               '1]/div/div[2]/div/div/div/form/div[1]/div/div/div/div/div/input').click()
            time.sleep(5)
            os_variant = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[5]/span')
            info1 = os_variant.text
            os_variant.click()
            time.sleep(5)
            # 点击添加地基工程
            # 点击下拉三角
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                               '1]/div/div[2]/div/div/div/form/div[2]/div/div/div/div['
                                               '2]/div/span/span').click()
            time.sleep(1)
            # 选第一个
            ground_project1 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[1]/span')
            info2 = ground_project1.text
            ground_project1.click()
            time.sleep(1)
            # 选第二个
            ground_project2 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[2]/span')
            info3 = ground_project2.text
            ground_project2.click()
            time.sleep(1)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                               '1]/div/div[2]/div/div/div/form/div[2]/div/div/div/div['
                                               '2]/div/span/span').click()
            # 点击 arch下拉框
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div['
                                               '1]/div/div[2]/div/div/div/form/div[3]/div/div/div/div/div/input').click()
            time.sleep(5)
            arch = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[5]/div/div/div[1]/ul/li[1]/span')
            info4 = arch.text
            arch.click()
            time.sleep(5)

            # 点击确认按钮
            self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/div['
                                     '2]/div/div/footer/span/button[1]/span').click()
            return [info1, "{} ".format(info2), "{} ".format(info3), info4]
        except Exception as e:
            message = '添加构建目标页面元素定位失败'
            LOG_ERROR("error: found build target view element timeout")
            print(e)
            LOG_INFO(message)


    def check_target_info(self):
        """
       获取build_target列表信息
       """
        try:
            info1 = self.driver.find_element(By.XPATH, '//div[3][@class="el-table__body-wrapper"]/div/div[1]/div/table/tbody/tr[1]/td[1]/div').text
            info2 = self.driver.find_element(By.XPATH, '//table[@class="el-table__body"]/tbody/tr[1]/td[3]/div/span[1]').text
            info3 = self.driver.find_element(By.XPATH, '//table[@class="el-table__body"]/tbody/tr[1]/td[3]/div/span[2]').text
            info4 = self.driver.find_element(By.XPATH, '//div[3]/div/div[1]/div/table/tbody/tr[1]/td[2]/div').text
            return [info1, info2, info3, info4]
        except Exception as e:
            message = 'build_target列表信息定位失败'
            LOG_ERROR("error: found build target list information timeout")
            print(e)
            LOG_INFO(message)